/**
 * Copyright 2008 Mathias Doenitz, http://lis.to/
 *
 * This file is part of the lis.to java desktop client. The lis.to java desktop client is free software: you can
 * redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free
 * Software Foundation, either version 3 of the License, or (at your option) any later version.
 *
 * The lis.to java desktop client is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along with the lis.to java desktop client.
 * If not, see http://www.gnu.org/licenses/
 */

package listo.client.model;

import com.google.common.collect.Lists;
import com.thoughtworks.xstream.annotations.XStreamConverter;
import com.thoughtworks.xstream.converters.collections.CollectionConverter;
import com.thoughtworks.xstream.mapper.Mapper;
import listo.utils.types.Copyable;

import java.util.ArrayList;
import java.util.List;

@XStreamConverter(Tasks.Converter.class)
public class Tasks extends ArrayList<Task> implements Cloneable, Copyable {

    public Tasks() {}

    public Tasks(Object[] objects) {
        if (objects != null) {
            for (Object object : objects) {
                add((Task) object);
            }
        }
    }

    public boolean contains(ObjectId id){
        for (Task task : this) {
            if (task.getId().equals(id)) return true;
        }
        return false;
    }

    public boolean allActive() {
        for (Task task : this) {
            if (!task.hasFolders()) return false;
        }
        return true;
    }

    public boolean allDeleted() {
        for (Task task : this) {
            if (task.hasFolders()) return false;
        }
        return true;
    }

    public boolean allCompleted() {
        for (Task task : this) {
            if (!task.isCompleted()) return false;
        }
        return true;
    }

    public boolean allHaveAtLeastTwoFolders() {
        for (Task task : this) {
            if (task.getFolderCount() < 2) return false;
        }
        return true;
    }

    public ObjectId[] toObjectIds() {
        ObjectId[] ids = new ObjectId[size()];
        for (int i = 0; i < ids.length; i++) {
            ids[i] = get(i).getId();
        }
        return ids;
    }

    public Tasks copy() {
        Tasks clone = new Tasks();
        for (Task task : this) clone.add(task.copy());
        return clone;
    }

    public List<ObjectId> getCommonFolders() {
        List<ObjectId> folders = Lists.newArrayList();
        boolean first = true;
        for (Task task : this) {
            if (first) {
                if (task.hasFolders()) folders.addAll(task.getFolders());
            } else {
                for (int i = 0; i < folders.size(); i++) {
                    if (!task.hasFolders(folders.get(i))) folders.remove(i--);
                }
            }
            first = false;
        }
        return folders;
    }

    // custom xstream converter
    public static class Converter extends CollectionConverter {

        public Converter(Mapper mapper) {
            super(mapper);
        }

        public boolean canConvert(Class type) {
            return type.equals(Tasks.class);
        }
    }
}
